<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
    <link rel="stylesheet" type="text/css" href="/assets/static/plugin/jquery-confirm/jquery-confirm.min.css"/>
    <link rel="stylesheet" type="text/css" href="/assets/static/admin/css/bootstrap.min.css"/>
    <link rel="stylesheet" type="text/css" href="/assets/static/admin/css/style.min.css"/>
</head>

<body>
<div class="container-fluid p-t-15">
    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-body">
                    <form>
                        <div class="form-group">
                            <label>旧密码</label>
                            <input type="password" class="form-control" maxlength="20" id="old_password"
                                   placeholder="输入账号的原登录密码"/>
                        </div>
                        <div class="form-group">
                            <label>新密码</label>
                            <input type="password" class="form-control" maxlength="20" id="new_password" placeholder="输入新的密码"/>
                        </div>
                        <div class="form-group">
                            <label>确认新密码</label>
                            <input type="password" class="form-control" maxlength="20" id="confirm_password"
                                   placeholder="再次输入新的密码"/>
                        </div>
                        <button type="button" id="btnOk" class="btn btn-primary">确认</button>
                        <button type="button" id="btnLoading" class="btn btn-primary" disabled style="display: none">
                            <span class="spinner-grow spinner-grow-sm" role="status" aria-hidden="true"></span>
                            执行中...
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript" src="/assets/static/admin/js/jquery.min.js"></script>
<script type="text/javascript" src="/assets/static/admin/js/bootstrap.bundle.min.js"></script>
<script type="text/javascript" src="/assets/static/plugin/bootstrap-maxlength/bootstrap-maxlength.min.js"></script>
<script type="text/javascript" src="/assets/static/plugin/jquery-confirm/jquery-confirm.min.js"></script>
<script type="text/javascript" src="/assets/bootstrap/js/bootstrap-multitabs/multitabs.min.js"></script>
<script type="text/javascript" src="/assets/static/admin/js/httpclient.js"></script>
<script type="text/javascript" src="/assets/static/admin/js/authorization/md5.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("input#old_password, input#new_password, input#confirm_password").maxlength({
            warningClass: "badge badge-info",
            limitReachedClass: "badge badge-warning"
        });

        $('#btnOk').on('click', function () {
            const old_password = $("#old_password").val();
            if (old_password === "") {
                $.alert({
                    title: '温馨提示',
                    icon: 'mdi mdi-alert',
                    type: 'orange',
                    content: '请输入账号的原登录密码。',
                });
                return false;
            }

            const new_password = $("#new_password").val();
            if (new_password === "") {
                $.alert({
                    title: '温馨提示',
                    icon: 'mdi mdi-alert',
                    type: 'orange',
                    content: '请输入新的密码。',
                });
                return false;
            }

            const confirm_password = $("#confirm_password").val();
            if (confirm_password === "") {
                $.alert({
                    title: '温馨提示',
                    icon: 'mdi mdi-alert',
                    type: 'orange',
                    content: '请再次输入新的密码。',
                });
                return false;
            }

            if (new_password !== confirm_password) {
                $.alert({
                    title: '温馨提示',
                    icon: 'mdi mdi-alert',
                    type: 'orange',
                    content: '请确认两次密码一致。',
                });
                return false;
            }

            const postData = {
                old_password: md5(old_password),
                new_password: md5(new_password),
            };

            AjaxForm(
                "PATCH",
                "/api/admin/modify_password",
                postData,
                function () {
                    $(this).hide();
                    $("#btnLoading").show();
                },
                function () {
                    $("#btnLoading").hide();
                    $("#btnOk").show();

                    $.alert({
                        title: '操作成功',
                        icon: 'mdi mdi-check-decagram',
                        type: 'green',
                        content: '密码修改成功。',
                        buttons: {
                            okay: {
                                text: '关闭',
                                action: function () {
                                    if (window.frames.length !== parent.frames.length) {
                                        parent.window.open("/login", '_self');
                                    } else {
                                        window.open("/login", '_self');
                                    }
                                }
                            }
                        }
                    });
                },
                function (response) {
                    $("#btnLoading").hide();
                    $("#btnOk").show();
                    AjaxError(response);
                }
            );
        });
    })
</script>
</body>
</html>
